﻿Public Class CDefaultValueDiff
	'Constructor
	Public Sub New()
	End Sub
	Public Sub New(this As CDefaultValueList, ref As CDefaultValueList)
		Me.This = this
		Me.Ref = ref

		Me.Same = New CDefaultValueList()
		Me.Diff = New CDefaultValueList()
		Me.Missing = New CDefaultValueList()
		Me.Extra = New CDefaultValueList()

		For Each i As CDefaultValue In this
			If Not ref.Has(i.MD5) Then Extra.Add(i)
		Next
		For Each i As CDefaultValue In ref
			If Not this.Has(i.MD5) Then
				Missing.Add(i)
				Continue For
			End If

			Dim rCol As CDefaultValue = this.GetByHash(i.MD5)
			If rCol.Name.Equals(i.Name) Then
				Same.Add(i)
			Else
				Diff.Add(i)
			End If
		Next
	End Sub

	'Diff Info
	Public Same As CDefaultValueList
	Public Diff As CDefaultValueList 'Type or IsNull
	Public Missing As CDefaultValueList
	Public Extra As CDefaultValueList

	Public This As CDefaultValueList
	Public Ref As CDefaultValueList

	Public ReadOnly Property IsExact As Boolean
		Get
			Return Diff.Count + Missing.Count + Extra.Count = 0
		End Get
	End Property
	Public ReadOnly Property IsCloseEnough As Boolean
		Get
			Return Missing.Count + Extra.Count = 0
		End Get
	End Property

	'Presentation Logic
	Public Overrides Function ToString() As String
		If IsExact Then Return CUtilities.NameAndCount("*Exact Match", Same, "col")

		Dim list As New List(Of String)
		If IsCloseEnough Then
			list.Add(CUtilities.NameAndCount("*Close Enough", Same, "col"))
		Else
			list.Add("*Diff:")
		End If

		If Same.Count > 0 Then list.Add(CUtilities.NameAndCount("Same", Same))
		If Diff.Count > 0 Then list.Add(CUtilities.NameAndCount("Renamed", Diff))
		If Extra.Count > 0 Then list.Add(CUtilities.NameAndCount("Extra", Extra) & ": " & Extra.NamesAbc_)
		If Missing.Count > 0 Then list.Add(CUtilities.NameAndCount("Missing", Missing) & ": " & Missing.NamesAbc_)
		Return CUtilities.ListToString(list)
	End Function


	'Scripting
	Public Function ChangeScripts(drop As Boolean, Optional exact As Boolean = False) As List(Of String)
		Dim create As Boolean = Not drop

		Dim list As New List(Of String)
		If drop Then
			For Each i As CDefaultValue In Me.Extra
				list.Add(i.DropScript())
			Next
			If exact Then
				For Each i As CDefaultValue In Me.Diff
					list.Add(i.DropScript())
				Next
			End If
		End If

		If create Then
			For Each i As CDefaultValue In Me.Missing
				list.Add(i.CreateScript())
			Next
			If exact Then
				For Each i As CDefaultValue In Me.Diff
					list.Add(i.CreateScript())
				Next
			End If
		End If
		Return list
	End Function
End Class
